﻿namespace ECommon.Wrap
{
    /// <summary>
    /// 返回结果
    /// </summary>
    public partial class EResult
    {
        /// <summary>
        /// 结果码
        /// 1-100：成功；
        /// 其它：失败；
        /// </summary>
        public int Status { get; set; } = 10;
        /// <summary>
        /// 消息
        /// </summary>
        public string Msg { get; set; } = "成功";
        /// <summary>
        /// 是否成功
        /// </summary>
        /// <returns>判定的结果</returns>
        public bool IsSuccess
        {
            get
            {
                return Status <= 100 && Status >= 1;
            }
        }
        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="msg">成功消息</param>
        /// <returns>成功结果</returns>
        public static EResult Success(string msg = "")
        {
            if (msg == null || string.IsNullOrEmpty(msg.Trim()))
            {
                return new EResult();
            }
            else
            {
                return new EResult() { Msg = msg };
            }
        }
        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="msg">失败消息</param>
        /// <returns>失败结果</returns>
        public static EResult Fail(string msg = "")
        {
            if (msg == null || string.IsNullOrEmpty(msg.Trim()))
            {
                return new EResult() { Msg = "失败", Status = -100 };
            }
            else
            {
                return new EResult() { Msg = msg };
            }
        }
        /// <summary>
        /// 成功
        /// </summary>
        /// <typeparam name="T">泛型模板</typeparam>
        /// <param name="val">成功的值</param>
        /// <param name="msg">成功消息</param>
        /// <returns>成功结果</returns>
        public static EResult<T> Success<T>(T val, string msg = "")
        {
            if (msg == null || string.IsNullOrEmpty(msg.Trim()))
            {
                return new EResult<T>() { Data = val, Msg = "成功" };
            }
            else
            {
                return new EResult<T>() { Data = val, Msg = msg };
            }
        }
        /// <summary>
        /// 失败
        /// </summary>
        /// <typeparam name="T">泛型模板</typeparam>
        /// <param name="msg">失败消息</param>
        /// <returns>失败结果</returns>
        public static EResult<T> Fail<T>(string msg = "")
        {
            if (msg == null || string.IsNullOrEmpty(msg.Trim()))
            {
                return new EResult<T>() { Msg = "失败" };
            }
            else
            {
                return new EResult<T>() { Msg = msg, Status = -100 };
            }
        }
    }
    /// <summary>
    /// 泛型返回结果
    /// </summary>
    /// <typeparam name="T">泛型</typeparam>
    public partial class EResult<T> : EResult
    {
        /// <summary>
        /// 数据
        /// </summary>
        public T Data { get; set; } = default(T);
    }
}
